<!-- 历史任务 -->
<template>
  <div class="past-task">
    <!-- 顶部标题栏组件 -->
    <top-header :leftArrow="leftArrow"></top-header>

    <!-- 任务清单 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in serList"
        :key="index"
        @click="toDetail(item)">
        <template #title>
          <div class="van-title">
            <div class="van-top">
              <van-tag plain type="primary" color="burlywood">{{ index + 1 }}</van-tag>
              <!-- 金钱的图标变色 -->
              <van-icon name="gold-coin-o" color="#cccccc" v-if="item.freesState === 0"/>
              <van-icon name="gold-coin-o" color="#44cef6" v-else-if="item.freesState === 1" />
              <van-icon name="gold-coin-o" color="#ffa400" v-else-if="item.freesState === 2"/>
              <van-icon name="gold-coin-o" color="#00e500" v-else-if="item.freesState === 3" />
              <van-icon name="gold-coin-o" color="red" v-else />
              <span class="custom-title">{{ item.date | filterTime }}</span>
            </div>
            <span class="cust-title">{{ item.cust_name }}</span>
          </div>
        </template>
        <template #label>
          <van-tag plain type="primary" color="burlywood">{{ item.deliverMan }}</van-tag>
          <span class="custom-title">{{ item.request }}</span>
        </template>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { TopHeader } from '@/components'
import { getMyHistorySer } from '@/request/api'
export default {
  name: 'MyTask',
  components: {
    TopHeader
  },
  filters: {
    filterTime (val) {
      return val.slice(0, 10)
    }
  },
  data () {
    return {
      leftArrow: true, // 顶部标题栏组件是否有返回按钮
      serList: [], // 我的任务数据
      serCount: 0, // 我的日志数据总量
      loading: false,
      finished: false,
      pageIndex: 0,
      userId: sessionStorage.getItem('userId') // 用户id
    }
  },
  created () {
    const param = { custName: '', staffId: this.userId, page: this.pageIndex, pageCount: 10 }
    // 获取历史任务列表
    getMyHistorySer(param).then(res => {
      console.log(res)
      this.serList = res.result.myHisSer
      this.serCount = res.result.count
    })
  },
  methods: {
    onLoad () {
      // 异步更新数据
      setTimeout(() => {
        this.pageIndex++
        const param = { custName: '', staffId: this.userId, page: this.pageIndex, pageCount: 10 }
        getMyHistorySer(param).then(res => {
          this.serList = [...this.serList, ...res.result.myHisSer]
        })
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (10 * (this.pageIndex + 1) >= this.serCount) {
          this.finished = true
        }
      }, 1000)
    },
    // 页面跳转到详情
    toDetail (value) {
      console.log('跳转到详情页', value)
      sessionStorage.setItem('SER_LIST', JSON.stringify(value))
      sessionStorage.setItem('History_Status', true)
      this.$router.push({ path: '/taskDetail' })
    }
  }
}
</script>

<style scoped lang="less">
.past-task {
  width: 100%;
  height: 100%;
  .van-list {
    overflow-y: auto;
    overflow-x: hidden;
    height: calc(100% - 1.2rem);
    .van-cell {
      .van-cell__title {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        .van-title {
          display: flex;
          .van-top {
            flex: 1;
            display: flex;
            align-items: center;
            .van-icon {
              width: 0.6rem;
            }
          }
          .cust-title {
            flex: 1;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            text-align: right;
          }
        }
        .van-tag {
          margin-right: 2px;
        }
        .van-cell__label {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}
</style>
